package com.xbongbong.workflow.service.help.saas;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.PaySheetTypeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.SerialNoHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.AddMuchFieldPojo;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.service.WorkflowOperatorDataService;
import com.xbongbong.paas.service.workflow.WorkflowDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.PackageStatusEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.payplan.pojo.dto.PayPlanDTO;
import com.xbongbong.pro.payplansheet.pojo.dto.batch.PayPlanSheetDTO;
import com.xbongbong.pro.payplansheet.pojo.vo.batch.PayPlanSheetAmountVO;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.help.FundHelp;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.service.PayPlanService;
import com.xbongbong.saas.service.toolbox.help.PackageStatusHelp;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.User;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 *
 * @author 魏荣杰
 *
 */
@Component
@Slf4j
public class PurchaseForPayPlanHelp {

    @Resource
    private PackageStatusHelp packageStatusHelp;
    @Resource
    private PayPlanService payPlanService;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private UserModel userModel;
    @Resource
    private SerialNoHelp serialNoHelp;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private WorkflowOperatorDataService workflowOperatorDataService;
    @Resource
    private FundHelp fundHelp;
    @Resource
    private WorkflowDataService workflowDataService;

    public void savePayPlan(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        if (saasNeedRedundantAttrPojo != null && saasNeedRedundantAttrPojo.getAddBatchDTO() != null) {
            String corpid = workflowTransferPOJO.getCorpid();
            Integer isImport = workflowTransferPOJO.getIsImport();
            Data pojoData = workflowTransferPOJO.getData();
            JSONObject data = pojoData.getData();
            Long purchaseId = pojoData.getId();
            User user = workflowTransferPOJO.getUser();


            List<AddMuchFieldPojo> addBatchList = saasNeedRedundantAttrPojo.getAddBatchDTO().getAddBatchList();

            if (!addBatchList.isEmpty() && !Objects.equals(isImport, BasicConstant.ONE)) {
                //非导入时判断数据量限制
                Integer oneStatus = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.ONE_FORM_DATA_NUM, addBatchList.size(), XbbRefTypeEnum.PAY_PLAN,
                        null);
                Integer allStatus = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.TOTAL_FORM_DATA_NUM, addBatchList.size());
                if ( !Objects.equals(oneStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(oneStatus, PackageStatusEnum.WARNING.getCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.SINGLE_TABLE_NUM_LIMIT);
                }

                if ( !Objects.equals(allStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(allStatus, PackageStatusEnum.WARNING.getCode()) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.TOTAL_TABLE_NUM_LIMIT);
                }
            }

            for (AddMuchFieldPojo addMuchFieldPojo : addBatchList) {
                addMuchFieldPojo.getData().put(PayPlanEnum.LINK_PURCHASE.getAttr(), purchaseId.toString());
                addMuchFieldPojo.getData().put(PayPlanEnum.LINK_PURCHASE_LINK_TEXT.getAttr(), data.getString(FieldTypeEnum.SERIALNO.getAlias()));
            }

            PayPlanDTO payPlanDTO = new PayPlanDTO();
            payPlanDTO.setIsImport(isImport);
            payPlanDTO.setParentNew(true);
            BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddBatchDTO(), payPlanDTO);
            payPlanDTO.setBelonger(FastJsonHelper.getStringOrDefaultFromFormData(data, PurchaseEnum.SIGNER_ID.getAttr(),""));
            payPlanDTO.setProcessCreatorId(user.getUserId());
            payPlanDTO.setPlatform(PlatFormEnum.INTERNAL.getValue());

            payPlanService.payPlan(payPlanDTO);
        }
    }

    public void savePayPlanSheet(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {

        Integer isImport = workflowTransferPOJO.getIsImport();
        String corpid = workflowTransferPOJO.getCorpid();
        Data pojoData = workflowTransferPOJO.getData();
        JSONObject data = pojoData.getData();
        Long purchaseId = pojoData.getId();
        User user = workflowTransferPOJO.getUser();

//        Double realPayTotalAmount = 0.0D;
        Long supplierId = FastJsonHelper.getLongOrDefaultFromFormData(data, PurchaseEnum.SUPPLIER_ID.getAttr(),0L);
        PayPlanSheetDTO payPlanSheetDTO = new PayPlanSheetDTO();
        payPlanSheetDTO.setIsImport(isImport);
        payPlanSheetDTO.setParentNew(true);
        payPlanSheetDTO.setProcessCreatorId(user.getUserId());

        List<AddMuchFieldPojo> writeOffPreSheets = new ArrayList<>();
        List<AddMuchFieldPojo> writeOffSheets = new ArrayList<>();

        if (saasNeedRedundantAttrPojo != null && saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO() != null) {
            writeOffPreSheets = saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO().getAddBatchList();
        }

        if (saasNeedRedundantAttrPojo != null && saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO() != null) {
            writeOffSheets = saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO().getAddBatchList();
        }
        Integer count  = writeOffPreSheets.size() + writeOffSheets.size();
        if (!Objects.equals(isImport, BasicConstant.ONE)) {
            //非导入时判断数据量限制
            Integer oneStatus = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.ONE_FORM_DATA_NUM, count, XbbRefTypeEnum.PAY_PLAN, null);
            Integer allStatus = packageStatusHelp.getPackageStatus(corpid, ValueAddedTypeEnum.TOTAL_FORM_DATA_NUM, count);
            if ( !Objects.equals(oneStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(oneStatus, PackageStatusEnum.WARNING.getCode()) ) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.SINGLE_TABLE_NUM_LIMIT);
            }

            if ( !Objects.equals(allStatus, PackageStatusEnum.NORMAL.getCode()) && !Objects.equals(allStatus, PackageStatusEnum.WARNING.getCode()) ) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100074, I18nStringConstant.TOTAL_TABLE_NUM_LIMIT);
            }
        }


        PurchaseEntityExt purchaseEntityExt = purchaseModel.getByKey(purchaseId, corpid);

        if (saasNeedRedundantAttrPojo != null && saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO() != null) {
            payPlanSheetDTO.setLinkContractEntity(purchaseEntityExt);
            BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO(), payPlanSheetDTO);
            for (AddMuchFieldPojo addMuchFieldPojo : payPlanSheetDTO.getAddBatchList()) {
                addMuchFieldPojo.getData().put(PaySheetEnum.LINK_PURCHASE.getAttr(), Arrays.asList(purchaseId.toString()));
                addMuchFieldPojo.getData().put(PaySheetEnum.PURCHASE_LINK_TEXT.getAttr(), Arrays.asList(purchaseEntityExt.getSerialNo()));
            }
            payPlanSheetDTO.setProcessCreatorId(user.getUserId());
            PayPlanSheetAmountVO payPlanSheetAmountVO = payPlanSheetAddBatch(payPlanSheetDTO, PaySheetTypeEnum.WRITE_OFF_PREPAY, supplierId);
//            realPayTotalAmount = Arith.add(realPayTotalAmount, payPlanSheetAmountVO.getRealPayTotalAmount());
        }
        if (saasNeedRedundantAttrPojo != null && saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO() != null) {
            payPlanSheetDTO.setLinkContractEntity(purchaseEntityExt);
            BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO(), payPlanSheetDTO);
            payPlanSheetDTO.setProcessCreatorId(user.getUserId());
            for (AddMuchFieldPojo addMuchFieldPojo : payPlanSheetDTO.getAddBatchList()) {
                addMuchFieldPojo.getData().put(PaySheetEnum.LINK_PURCHASE.getAttr(), Collections.singletonList(purchaseId.toString()));
                addMuchFieldPojo.getData().put(PaySheetEnum.PURCHASE_LINK_TEXT.getAttr(), Arrays.asList(purchaseEntityExt.getSerialNo()));
            }
            PayPlanSheetAmountVO payPlanSheetAmountVO = payPlanSheetAddBatch(payPlanSheetDTO, PaySheetTypeEnum.WRITE_OFF_PAY, supplierId);
//            realPayTotalAmount = Arith.add(realPayTotalAmount, payPlanSheetAmountVO.getRealPayTotalAmount());
        }

//        if (realPayTotalAmount > 0.0D){
//             更新现金毛利
//            updateProfit(corpid, realPayTotalAmount, purchaseId, purchaseEntityExt);
//        }
    }

    public void updatePayPlan(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        if (saasNeedRedundantAttrPojo != null) {
            String corpid = workflowTransferPOJO.getCorpid();
            Integer isImport = workflowTransferPOJO.getIsImport();
            Data pojoData = workflowTransferPOJO.getData();
            User user = workflowTransferPOJO.getUser();
            Long dataId = pojoData.getId();
            JSONObject data = pojoData.getData();

            Long supplierId = FastJsonHelper.getLongOrDefaultFromFormData(data, PurchaseEnum.SUPPLIER_ID.getAttr(), 0L);
            if (saasNeedRedundantAttrPojo.getAddBatchDTO() != null) {
                PayPlanDTO payPlanDTO = new PayPlanDTO();
                BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddBatchDTO(), payPlanDTO);
                payPlanDTO.setIsImport(isImport);
                payPlanDTO.setProcessCreatorId(user.getUserId());
                payPlanDTO.setPlatform(PlatFormEnum.INTERNAL.getValue());
                payPlanService.payPlan(payPlanDTO);
            }
//            Double realPayPlanTotalAmount = 0.0D;
            if (saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO() != null) {
                PayPlanSheetDTO payPlanSheetDTO = new PayPlanSheetDTO();
                BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddWriteOffPrepaymentBatchDTO(), payPlanSheetDTO);
                payPlanSheetDTO.setProcessCreatorId(user.getUserId());
                PayPlanSheetAmountVO writeOffPrepaymentAmountVO = payPlanSheetAddBatch(payPlanSheetDTO, PaySheetTypeEnum.WRITE_OFF_PREPAY, supplierId);
//                realPayPlanTotalAmount = writeOffPrepaymentAmountVO.getRealPayTotalAmount();
            }
            if (saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO() != null) {
                PayPlanSheetDTO payPlanSheetDTO = new PayPlanSheetDTO();
                BeanUtil.copyProperties(saasNeedRedundantAttrPojo.getAddPaymentSheetBatchDTO(), payPlanSheetDTO);
                payPlanSheetDTO.setProcessCreatorId(user.getUserId());
                PayPlanSheetAmountVO writeOffAmountVO = payPlanSheetAddBatch(payPlanSheetDTO, PaySheetTypeEnum.WRITE_OFF_PAY, supplierId);
                Double addPaymentAmount = writeOffAmountVO.getRealPayTotalAmount();
//                realPayPlanTotalAmount = Arith.add(realPayPlanTotalAmount, addPaymentAmount);
            }
//            if (realPayPlanTotalAmount>0.0D) {
//                PurchaseEntityExt purchaseEntityExt = new PurchaseEntityExt();
//                purchaseEntityExt.setData(data);
//                updateProfit(corpid, realPayPlanTotalAmount, dataId, purchaseEntityExt);
//            }
        }
    }

    private void updateProfit(String corpid, Double realPayTotalAmount, Long purchaseId, PurchaseEntityExt purchaseEntityExt) throws XbbException {
        PaasFormDataEntity purchase;
        if ( Objects.nonNull(purchaseEntityExt) ) {
            purchase = purchaseEntityExt;
        } else {
            purchase = purchaseModel.getByKey(purchaseId, corpid);
        }

        if ( Objects.isNull(purchase) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        Double finishMoney = getDoubleOrDefaultFromFormData(purchase.getData(), PurchaseEnum.FINISH_AMOUNT.getAttr(), 0D);
        Double amount = getDoubleOrDefaultFromFormData(purchase.getData(), PurchaseEnum.TOTAL_MONEY.getAttr(), 0D);
        Double badAmount = getDoubleOrDefaultFromFormData(purchase.getData(), PurchaseEnum.BAD_AMOUNT.getAttr(), 0D);
        JSONObject jsonObject = new JSONObject();
        Double newFinishMoney = Arith.add(finishMoney, realPayTotalAmount);
        Double newUnFinishMoney = Arith.sub(Arith.sub(amount, Arith.add(finishMoney, realPayTotalAmount)), badAmount);
        jsonObject.put(PurchaseEnum.FINISH_AMOUNT.getAttr(), newFinishMoney);
        jsonObject.put(PurchaseEnum.UN_FINISH_AMOUNT.getAttr(), newUnFinishMoney);
        if ( Double.compare(amount, BasicConstant.ZERO) == BasicConstant.ZERO ) {
            jsonObject.put(PurchaseEnum.RECEINE_PERCENT.getAttr(), BasicConstant.ZERO);
        } else {
            jsonObject.put(PurchaseEnum.RECEINE_PERCENT.getAttr(), Arith.round(Arith.div(newFinishMoney, amount), 4));
        }
        UpdateDataEntity updateData = ExplainUtil.getUpdateData(purchaseId, jsonObject, corpid);
        purchaseModel.updateBatch(Collections.singletonList(updateData), corpid);
    }


    private PayPlanSheetAmountVO payPlanSheetAddBatch(PayPlanSheetDTO payPlanSheetDTO, PaySheetTypeEnum paySheetTypeEnum, Long supplierId) throws XbbException {
        String corpid = payPlanSheetDTO.getCorpid();
        String userId = payPlanSheetDTO.getProcessCreatorId();

        PayPlanSheetAmountVO paymentSheetAmountVO = new PayPlanSheetAmountVO();
        // 批量新增流水号集合
        List<AddMuchFieldPojo> addBatchList = payPlanSheetDTO.getAddBatchList();
        // 开启严控模式后生成流水号
        PaasFormExplainEntity formExplainEntity = paasFormExplainModel.getByFormId(payPlanSheetDTO.getFormId(), payPlanSheetDTO.getCorpid());
        if (Objects.isNull(formExplainEntity)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
        }
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(formExplainEntity.getExplains(), null);
        FieldAttrEntity fieldAttrEntity = explainMap.get(PaymentEnum.PAYMENT_NO.getAttr());
        if (Objects.nonNull(fieldAttrEntity)) {
            FormDataAddDTO formDataAddDTO = new FormDataAddDTO();
            BeanUtil.copyProperties(payPlanSheetDTO, formDataAddDTO);
            String serialNo = "";
            for (AddMuchFieldPojo addMuchFieldPojo : addBatchList) {
                if (Objects.equals(fieldAttrEntity.getStrictController(), BasicConstant.ONE) && StringUtil.isEmpty(addMuchFieldPojo.getSerialNo())) {
                    serialNo = serialNoHelp.getSerialNo(serialNo, corpid, formDataAddDTO.getFormId(), fieldAttrEntity);
                    addMuchFieldPojo.setSerialNo(serialNo);
                }
            }
        }

        try {
            String lang = LocaleContextHolder.getLocale().toString();

            // 删除逻辑
            List<AddMuchFieldPojo> delBatchList = payPlanSheetDTO.getDelBatchList();
            if (CollectionsUtil.isNotEmpty(delBatchList)) {
                for (AddMuchFieldPojo addMuchFieldPojo : delBatchList) {
                    FormDataDeleteBatchDTO formDataDeleteBatchDTO = new FormDataDeleteBatchDTO();
                    formDataDeleteBatchDTO.setDataIdList(Collections.singletonList(addMuchFieldPojo.getId()));
                    formDataDeleteBatchDTO.setIsBusinessRule(0);
                    formDataDeleteBatchDTO.setCorpid(corpid);
                    formDataDeleteBatchDTO.setUserId(userId);
                    formDataDeleteBatchDTO.setPlatform(PlatFormEnum.INTERNAL.getValue());
                    formDataDeleteBatchDTO.setBusinessType(formExplainEntity.getBusinessType());
                    formDataDeleteBatchDTO.setFormId(formExplainEntity.getFormId());
                    formDataDeleteBatchDTO.setAppId(formExplainEntity.getAppId());
                    formDataDeleteBatchDTO.setMenuId(formExplainEntity.getMenuId());
                    formDataDeleteBatchDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    workflowOperatorDataService.delete(formDataDeleteBatchDTO);
                }
            }


            // 插入数据
            Double amount = 0.0D;
            if(CollectionsUtil.isNotEmpty(addBatchList)) {
                for (AddMuchFieldPojo addMuchFieldPojo : addBatchList) {
                    JSONObject data = addMuchFieldPojo.getData();
                    Double realPaymentAmount = FundHelp.getSheetAmount4PaymentSheet(data, paySheetTypeEnum.getCode());
                    amount = Arith.add(amount, realPaymentAmount);

                    FormDataAddDTO formDataAddDTO = new FormDataAddDTO();
                    //代表是采购合同进了工作流，那么当采购合同通过之后，付款单不管进不进工作流其实都不应该走团队权限校验了
                    formDataAddDTO.setFromApproval(true);
                    formDataAddDTO.setAppId(formExplainEntity.getAppId());
                    formDataAddDTO.setFormId(formExplainEntity.getFormId());
                    formDataAddDTO.setMenuId(formExplainEntity.getMenuId());
                    formDataAddDTO.setSerialNo(addMuchFieldPojo.getSerialNo());
                    formDataAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    formDataAddDTO.setBusinessType(paySheetTypeEnum.getBusinessType());
                    formDataAddDTO.setSubBusinessType(paySheetTypeEnum.getBusinessType());
                    formDataAddDTO.setDataList(data);
                    formDataAddDTO.setIsBusinessRule(0);
                    formDataAddDTO.setCorpid(corpid);
                    formDataAddDTO.setUserId(userId);
                    formDataAddDTO.setPlatform(PlatFormEnum.INTERNAL.getValue());
                    JSONArray ownerJsonArray = data.getJSONArray(FieldTypeEnum.OWNERID.getAlias());
                    JSONArray coUserJsonArray = data.getJSONArray(FieldTypeEnum.COUSERID.getAlias());
                    if (Objects.nonNull(ownerJsonArray) && ownerJsonArray.size() > BasicConstant.ZERO) {
                        List<ReturnUserAndDepartmentPojo> ownerIds = ownerJsonArray.toJavaList(ReturnUserAndDepartmentPojo.class);
                        formDataAddDTO.setOwnerId(ownerIds);
                    }
                    if (Objects.nonNull(coUserJsonArray) && coUserJsonArray.size() > BasicConstant.ZERO) {
                        List<ReturnUserAndDepartmentPojo> couserIds = coUserJsonArray.toJavaList(ReturnUserAndDepartmentPojo.class);
                        formDataAddDTO.setCoUserId(couserIds);
                    }
                    workflowOperatorDataService.add(formDataAddDTO);
                }
            }
            paymentSheetAmountVO.setRealPayTotalAmount(amount);

            // 更新数据
            List<AddMuchFieldPojo> updateBatchList = payPlanSheetDTO.getUpdateBatchList();
            if(Objects.nonNull(updateBatchList) && !updateBatchList.isEmpty()){
                UserEntity userEntity = userModel.getByKeyIngoreDel(userId, corpid);
                UserVO userVO = new UserVO();
                BeanUtil.copyProperties(userEntity, userVO);
                for (AddMuchFieldPojo addMuchFieldPojo : updateBatchList) {
                    FormDataUpdateDTO formDataUpdateDTO = new FormDataUpdateDTO(formExplainEntity.getFormId(), formExplainEntity.getAppId(), formExplainEntity.getMenuId(), addMuchFieldPojo.getId(), addMuchFieldPojo.getData(), SaasMarkEnum.SAAS.getCode(), paySheetTypeEnum.getBusinessType(), paySheetTypeEnum.getBusinessType(), null, userVO, corpid, null, PlatFormEnum.INTERNAL.getValue());
                    workflowDataService.update(formDataUpdateDTO);
                }
            }
        } catch (Exception e) {
            log.error("com.xbongbong.workflow.service.help.saas.PurchaseForPayPlanHelp.payPlanSheetAddBatch 回款出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return paymentSheetAmountVO;
    }



}
